﻿using Microsoft.Expression.Drawing.Core;
using project.B12.application.Ioc;
using project.B12.IRepository;
using project.B12.Model.Models;
using project.B12.Model.Res;
using project.B12.ViewModels;
using software.Common.MVVM;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace project.B12.application.ViewModels.Setting
{
    public class MedicineViewModel:ObservableObject
    {
        private IMedicineRepository _medicineRepository;

        private PublicResourceViewModel _publicResourceViewModel;

        private List<Table_medicine> _medicines=new List<Table_medicine>();

        private int count;
        public int Count
        {
            get => count;
            set { count = value; CurrentPageChanged(); }
        }

        private int countPerPage = 14;

        public int CountPerPage
        {
            get => countPerPage;
            set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
        }

        private int currentPage;

        public int CurrentPage
        {
            get => currentPage;
            set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
        }

        private bool _isEnabled=true;
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                this.RaisePropertyChanged();
            }
        }

        private bool _cancelEnabled;
        public bool CancelEnabled
        {
            get { return _cancelEnabled; }
            set
            {
                _cancelEnabled=value;
                this.RaisePropertyChanged();
            }
        }

        private string _nameInput;
        public string NameInput
        {
            get { return _nameInput; }
            set
            {
                _nameInput = value;
                QueryMedicines(value);
                if(string.IsNullOrEmpty(value))
                    CurrentName = null;
                this.RaisePropertyChanged();
            }
        }

        private string _currentName;
        public string CurrentName
        {
            get { return _currentName; }
            set
            {
                _currentName = value;
                //QueryMedicines(value);
                this.RaisePropertyChanged();
            }
        }

        public RelayCommand Query => new RelayCommand(QueryIndistinct);

        public RelayCommand<object> Update => new RelayCommand<object>(UpdateMedicine);

        public RelayCommand<object> Delete => new RelayCommand<object>(DeleteMedicine);

        public RelayCommand Add => new RelayCommand(AddMedicine);

        public RelayCommand Cancel => new RelayCommand(CancelAdd);

        private ObservableCollection<Table_medicine> _currentMedicines=new ObservableCollection<Table_medicine>();
        public ObservableCollection<Table_medicine> CurrentMedicines
        {
            get { return _currentMedicines; }
            set
            {
                _currentMedicines = value;
                this.RaisePropertyChanged();
            }
        }

        public ObservableCollection<string> MedicineNames { get; set; }=new ObservableCollection<string>();

        public MedicineViewModel(IMedicineRepository medicineRepository)
        {
            _medicineRepository=medicineRepository;
            _publicResourceViewModel=ViewModelLocator.PublicResourceViewModel;
            Init();
        }

        public void Init()
        {
            _medicines = _medicineRepository.QueryAllMedicine();
            _medicines.ForEach(item => item.IsAdd = true);
            Count = _medicines.Count;
            CurrentMedicines = new ObservableCollection<Table_medicine>(_medicines.Take(CountPerPage));
            CurrentPage = 1;

            MedicineNames.Add("全部");
            foreach (var medicine in _medicines)
            {
                MedicineNames.Add(medicine.CN_Name);
            }
            _publicResourceViewModel.MedicineNames = MedicineNames;
            _publicResourceViewModel.Medicines = new ObservableCollection<Table_medicine>(_medicines);
        }

        private void CurrentPageChanged()
        {
            CurrentMedicines = new ObservableCollection<Table_medicine>(_medicines.Skip((CurrentPage - 1) * countPerPage).Take(countPerPage));
        }

        private void QueryMedicines(string name)
        {
            if (name == "全部"||string.IsNullOrEmpty(name))
            {
                CurrentPageChanged();
                return;
            }
            var medicines = _medicines.Where(o => o.CN_Name.Contains(name));
            CurrentMedicines = new ObservableCollection<Table_medicine>(medicines);
        }

        private void QueryIndistinct()
        {
            if (string.IsNullOrEmpty(NameInput))
            {
                CurrentPageChanged();
                return;
            }
            var medicines = _medicines.Where(o => o.CN_Name.Contains(NameInput)).ToList();
            CurrentMedicines = new ObservableCollection<Table_medicine>(medicines);
        }

        private void UpdateMedicine(object medicine)
        {
            Table_medicine temp = (Table_medicine)medicine;
            int exitCount= _medicines.Where(o=>o.Abbreviation==temp.Abbreviation).Count();
            if(exitCount>1)
            {
                MessageBox.Show(Resources.MedicineAbbrevExits);
                return;
            }
            if(string.IsNullOrEmpty(temp.CN_Name)&&string.IsNullOrEmpty(temp.EN_Name))
            {
                MessageBox.Show(Resources.MedicineAddError);
                return;
            }

            if(!MedicineNames.Contains(temp.CN_Name))
            {
                _publicResourceViewModel.MedicineNames = MedicineNames;
            }
            _medicineRepository.UpdateMedicine((Table_medicine)medicine);
            _medicines.Where(o => o.Id == temp.Id).ToList().First().IsUpdate = true;
            _publicResourceViewModel.Medicines = new ObservableCollection<Table_medicine>(_medicines);
            MessageBox.Show(Resources.UpdateSucess);
        }

        private void DeleteMedicine(object medicine)
        {
            MessageBoxResult result = MessageBox.Show(Resources.DeleteWaring, Resources.OperatorConfirm, MessageBoxButton.OKCancel, MessageBoxImage.Question);
            if (result == MessageBoxResult.Cancel)
                return;
            Table_medicine temp = (Table_medicine)medicine;
            temp.IsDelete = true;
            _medicineRepository.UpdateMedicine(temp);
            _medicines.Remove(temp);
            MedicineNames.Remove(temp.CN_Name);
            _publicResourceViewModel.MedicineNames = MedicineNames;
            _publicResourceViewModel.Medicines = new ObservableCollection<Table_medicine>(_medicines);
            CurrentMedicines.Remove(temp);
            MessageBox.Show(Resources.DeleteSuccess);
        }

        private void AddMedicine()
        {
            if(IsEnabled)
            {
                IsEnabled = false;
                CancelEnabled = true;
                CurrentMedicines.Add(new Table_medicine());
                return;
            }
            var medicine=CurrentMedicines.Where(o=>o.IsAdd==false).ToList().First();
            if(string.IsNullOrEmpty(medicine.CN_Name)&&string.IsNullOrEmpty(medicine.EN_Name))
            {
                MessageBox.Show(Resources.MedicineAddError);
                return;
            }

            bool exit= _medicines.Any(o => o.Abbreviation == medicine.Abbreviation);
            if(exit)
            {
                MessageBox.Show(Resources.MedicineExits);
                return;
            }
            _medicineRepository.AddMedicine(medicine);
            MedicineNames.Add(medicine.CN_Name);
            _publicResourceViewModel.MedicineNames = MedicineNames;
            medicine.IsAdd = true;
            _medicines.Add(medicine);
            _publicResourceViewModel.Medicines = new ObservableCollection<Table_medicine>(_medicines);
            IsEnabled = true;
            CancelEnabled = false;
            MessageBox.Show(Resources.AddSuccess);
        }

        private void CancelAdd()
        {
            if (!IsEnabled)
            {
                CurrentMedicines.RemoveLast();
                IsEnabled = true;
            }
        }
    }
}
